import os
import sys
import pandas as pd
from pandas import DataFrame
sys.path.append ('../')
import data
from data import Data

class DataManagement (): #数据管理

    def __init__(self) -> None:
        self.UnTaggedAction = None #未贴标签的行为数据集
        self.UnTaggedText = None #未贴标签的文本数据集
        self.TrainAction = None #本地行为缓冲数据集
        self.TrainText = None #本地文本缓冲数据集
        self.OM = None #数据操作模块

        self.OM = Data.ObjMemory()
        
        path = '../data/UnTaggedAction.pkl'
        self.UnTaggedAction = self.OM.LoadObj(path)

        path = '../data/UnTaggedText.pkl'
        self.UnTaggedText = self.OM.LoadObj(path)

        path = '../data/Local/TrainAction_Buffer.pkl'
        self.TrainAction = self.OM.LoadObj(path)

        path = '../data/Local/TrainText_Buffer.pkl'
        self.TrainText = self.OM.LoadObj(path)


    def ShowPrompt (self,index):
        col = self.UnTaggedAction.col
        return self.UnTaggedAction.Data.iloc[index,col-1]
    

    def ShowText (self,index):
        col = self.UnTaggedText.col
        return self.UnTaggedText.Data.iloc[index,col-1]


    def UpdateData (self,index,emotion,hidden,activatings,solutions,actions,words): #更新本地缓冲数据集
        self.TrainAction.AddAction (actions)
        self.TrainAction.AddSolution (solutions)
        r = self.TrainAction.AddActivating (activatings)
        nw = self.TrainText.AddWords (words)

        if r == 1: #有新的诱因
            activating = len(self.TrainAction.activatings) - 1
        else:
            for i in range (0,len(self.TrainAction.activatings)):
                if activatings[0] == self.TrainAction.activatings[i]:
                    activating = i
                    break

        dict = {}
        for i in range (0,self.TrainAction.Data.shape[1]):
            dict[self.TrainAction.Data.iloc[:,i].name] = 0
        
        sample = pd.DataFrame ([dict])

        for i in range (5,sample.shape[1]): #初始化样本
            sample.iloc[0,i] = 0

        o_sample = self.UnTaggedAction.Data.iloc[index:index+1] #预测的样本
        text = self.UnTaggedText.Data.iloc[index,self.UnTaggedText.col-1] #输入的文字

        for i in range (5,self.UnTaggedAction.solution_start): #编辑新样本行为列
            sample.iloc[0,i] = o_sample.iloc[0,i]

        former = self.UnTaggedAction.Data.iloc[0:index]
        latter = self.UnTaggedAction.Data.iloc[index+1:self.UnTaggedAction.datasize]
        self.UnTaggedAction.Data = pd.concat([former,latter])
        self.UnTaggedAction.datasize -= 1 #在未贴标签的数据集中去除该样本

        former = self.UnTaggedText.Data.iloc[0:index]
        latter = self.UnTaggedText.Data.iloc[index+1:self.UnTaggedText.datasize]
        self.UnTaggedText.Data = pd.concat([former,latter])
        self.UnTaggedText.datasize -= 1 #在未贴标签的数据集中去除该样本

        sample.iloc[0,2] = emotion
        sample.iloc[0,3] = activating
        sample.iloc[0,4] = hidden

        for i in range (self.UnTaggedAction.solution_start,self.TrainAction.solution_start):
            sample.iloc[0,i] = 1 #让新增的行为列为1

        for i in range (0,len(solutions)): #构建样本的解决部分
            for j in range (0,len(self.TrainAction.solutions)):
                if solutions[i] == self.TrainAction.solutions[j]:
                    sample.iloc[0,self.TrainAction.solution_start + j] = 1
                    break
        
        text_sample = self.TrainText.TextSample(text)
        if emotion == 0 or emotion == 4:
            emotion = 0
        else:
            emotion = 1

        text_sample.iloc[0,0] = (hidden + emotion) % 2

        for i in range (text_sample.shape[1]-nw,text_sample.shape[1]): #确保输入的词语在样本中存在
            text_sample.iloc[0,i] = 1
        print (text_sample)

        self.TrainAction.AddSample (sample)
        self.TrainText.AddSample (text_sample)

        path = '../data/Local/TrainAction_Buffer.pkl'
        self.OM.SaveObj(self.TrainAction,path)

        path = '../data/Local/TrainText_Buffer.pkl'
        self.OM.SaveObj(self.TrainText,path)

        path = os.getcwd()
        path1 = path + '/../data'
        os.chdir(path1)
        self.UnTaggedAction.Update ()
        self.UnTaggedText.Update ()
        os.chdir(path)

        path = '../data/UnTaggedAction.pkl'
        self.OM.SaveObj(self.UnTaggedAction,path)

        path = '../data/UnTaggedText.pkl'
        self.OM.SaveObj(self.UnTaggedText,path)